package com.oxy.util;

import java.io.Serializable;
import java.util.Enumeration;
import java.util.Properties;

import com.oxy.common.util.GenericPropLoader;

public final class ConfigManager implements Serializable{
	private static final long serialVersionUID = -521688709408163809L;
	
	public static class AppCfg extends Properties {
		private static final long serialVersionUID = 5890773485981274233L;
		
		public String CLASSPATH;
		public String ROOTPATH;
		public String ROOTURL;
		
		public String APP_NAME;
		public String ENCODING;
		public String PAGER_PAGESIZE;
		
		public String CFG_LOG;
		public String CFG_DB;
		public String CFG_STRUTS;
		
		public String FSPATH_STATIC;
		public String FSPATH_STATIC_IMG;
		public String FSPATH_STATIC_CSS;
		public String FSPATH_STATIC_JS;
		
		public String URL_STATIC;
		public String URL_STATIC_IMG;
		public String URL_STATIC_CSS;
		public String URL_STATIC_JS;
	}
	public static class DbCfg extends Properties {
		private static final long serialVersionUID = -6468413815822358143L;
		
		public String DRIVER_CLASS;
		public String URL;
		public String USERID;
		public String PASSWORD;
	}
	public static class StrutsCfg extends Properties {
		private static final long serialVersionUID = 6760925120335472998L;
		
		public String ACTION_EXT_NM;
		public String I18N_RESOURCE;
		public String I18N_ENCODING;
		public String LOCALE;
	}
	public static class LogCfg extends Properties {
		private static final long serialVersionUID = -7873586328591938859L;
		public static String LOG_COMMON = "common";
		public static String LOG_ACCESS = "access";
		public static String LOG_OTHER = "other";
	}
	
	private String appcfgFile = null;
	
	private AppCfg appcfg = null;
	private DbCfg dbcfg = null;
	private StrutsCfg strutscfg = null;
	private LogCfg logcfg = null;
	
	public ConfigManager(String appcfgFile) {
		this.appcfgFile = appcfgFile;
		init();
	}
	
	private void init(){
		GenericPropLoader cfgLoader = new GenericPropLoader();
		// init application config
		appcfg = new ConfigManager.AppCfg();
		Properties prop = cfgLoader.getFinalProperties(appcfgFile);
		Enumeration<Object> keys = prop.keys();
		while(keys.hasMoreElements()) {
			String k = (String)keys.nextElement();
			appcfg.setProperty(k, prop.getProperty(k));
		}
		appcfg.CLASSPATH = appcfg.getProperty("classpath", "");
		appcfg.ROOTPATH = appcfg.getProperty("rootpath", "");
		appcfg.ROOTURL = appcfg.getProperty("rooturl", "");
		
		appcfg.APP_NAME = appcfg.getProperty("app.name", "");
		
		appcfg.CFG_LOG = appcfg.getProperty("cfgfile.log", "");
		appcfg.CFG_DB = appcfg.getProperty("cfgfile.db", "");
		appcfg.CFG_STRUTS = appcfg.getProperty("cfgfile.struts", "");
		
		appcfg.FSPATH_STATIC = appcfg.getProperty("fspath.static", "");
		appcfg.FSPATH_STATIC_IMG = appcfg.getProperty("fspath.static.img", "");
		appcfg.FSPATH_STATIC_CSS = appcfg.getProperty("fspath.static.css", "");
		appcfg.FSPATH_STATIC_JS = appcfg.getProperty("fspath.static.js", "");
		
		appcfg.URL_STATIC = appcfg.getProperty("url.static", "");
		appcfg.URL_STATIC_IMG = appcfg.getProperty("url.static.img", "");
		appcfg.URL_STATIC_CSS = appcfg.getProperty("url.static.css", "");
		appcfg.URL_STATIC_JS = appcfg.getProperty("url.static.js", "");
		
		appcfg.ENCODING = appcfg.getProperty("encoding", "");
		appcfg.PAGER_PAGESIZE = appcfg.getProperty("pager.pagesize", "");

		// init DB config
		dbcfg = new DbCfg();
		prop = cfgLoader.getFinalProperties(appcfg.CFG_DB);
		keys = prop.keys();
		while(keys.hasMoreElements()) {
			String k = (String)keys.nextElement();
			dbcfg.setProperty(k, prop.getProperty(k));
		}
		dbcfg.DRIVER_CLASS = dbcfg.getProperty("driverClassName", "");
		dbcfg.URL = dbcfg.getProperty("url", "");
		dbcfg.USERID = dbcfg.getProperty("userid", "");
		dbcfg.PASSWORD = dbcfg.getProperty("password", "");
		
		// init struts config
		strutscfg = new StrutsCfg();
		prop = cfgLoader.getFinalProperties(appcfg.CFG_STRUTS);
		keys = prop.keys();
		while(keys.hasMoreElements()) {
			String k = (String)keys.nextElement();
			strutscfg.setProperty(k, prop.getProperty(k));
		}
		strutscfg.ACTION_EXT_NM = strutscfg.getProperty("struts.action.extension", "");
		strutscfg.I18N_RESOURCE = strutscfg.getProperty("struts.custom.i18n.resources", "");
		strutscfg.I18N_ENCODING = strutscfg.getProperty("struts.i18n.encoding", "");
		strutscfg.LOCALE = strutscfg.getProperty("struts.locale", "");
		
		// init log config
		logcfg = new LogCfg();
		prop = cfgLoader.getFinalProperties(appcfg.CFG_LOG);
		keys = prop.keys();
		while(keys.hasMoreElements()) {
			String k = (String)keys.nextElement();
			logcfg.setProperty(k, prop.getProperty(k));
		}
	}
	
	public AppCfg App() {
		return appcfg;
	}
	public DbCfg Db() {
		return dbcfg;
	}
	public StrutsCfg Struts() {
		return strutscfg;
	}
	public LogCfg Log() {
		return logcfg;
	}
}